package com.py.pythink.controller;

import com.alibaba.fastjson.JSON;
import com.py.pythink.bo.ContentDto;
import com.py.pythink.config.RedisKeyConfig;
import com.py.pythink.config.SystemConfig;
import com.py.pythink.dao.UserCommentDao;
import com.py.pythink.dao.UserDetailDao;
import com.py.pythink.dao.UserSendDao;
import com.py.pythink.entity.User;
import com.py.pythink.entity.UserCommen;
import com.py.pythink.entity.UserDetail;
import com.py.pythink.third.JedisUtil;
import com.py.pythink.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/aoi/comment/")
@CrossOrigin
public class UserCommetController {
    @Autowired
    private UserCommentDao dao;
    @Autowired
    private UserDetailDao userDetailDao;
    @Autowired
    private UserSendDao userSendDao;

    @PostMapping("add")
    public R insert(HttpServletRequest request, int fuid, String content){
        String token = request.getHeader(SystemConfig.USER_TOKEN);
        User user = JSON.parseObject(JedisUtil.getStr(RedisKeyConfig.USER_TOKEN + token), User.class);
        System.err.println(user);
        UserDetail userDetail = userDetailDao.selectByUid(user.getId());
        UserCommen userCommen = new UserCommen();
        userCommen.setUid(user.getId());
        userCommen.setNickname(userDetail.getNickname());
        userCommen.setContent(content);
        userCommen.setFuid(fuid);
        if(dao.add(userCommen)>0){
           return R.ok(dao.selectAll(fuid));
        }
        return R.fail("亲，网络异常");
    }

}
